package com.sinocare.store.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sinocare.common.entity.Store;
import com.sinocare.common.result.Result;
import com.sinocare.common.result.ResultCode;
import com.sinocare.store.service.StoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/store")
@Api(tags = "连锁药房管理",description = "门店管理")
@CrossOrigin    //跨域请求
public class StoreController {

    @Resource
    private StoreService storeServiceImpl;

    /**
     * 总店搜索、查询
     * @param page
     * @param limit
     * @param searchParams
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "查询总店列表信息",notes = "模糊查询、分页",httpMethod = "GET")
    @RequestMapping(value = "queryOverallStore",method = RequestMethod.GET)
    public Result findOverallStores(Integer page,Integer limit,String searchParams) throws Exception{
        // 通过Jackson将JSON字符串转换成OverallStore对象
        ObjectMapper objectMapper = new ObjectMapper();

        Store store = null;

//        判断前端传递过来的搜索条件是否为null
        if (searchParams!=null){
//            将searchParams接收到的JSON字符串转换OverallStore对象
            store = objectMapper.readValue(searchParams, Store.class);
        }

//        PageHelper.clearPage();
//        考虑分页：mybatis中的分页插件：PageHelper
        Page page1 = PageHelper.startPage(page, limit);
        List<Store> stores = storeServiceImpl.queryOverallStore(store);
        return Result.ok(ResultCode.SUCCESS,page1.getTotal(),stores);
    }

    /**
     * 查询分店信息
     * @param page
     * @param limit
     * @param searchParams
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "查询分店列表信息",httpMethod = "GET")
    @RequestMapping(value = "querySubStore",method = RequestMethod.GET)
    public Result findSubStores(Integer page,Integer limit,String searchParams) throws Exception{

        ObjectMapper objectMapper = new ObjectMapper();
        Store store = null;
        if (searchParams!=null){
            store = objectMapper.readValue(searchParams, Store.class);
        }
        Page page1 = PageHelper.startPage(page, limit);
        List<Store> stores = storeServiceImpl.querySubStore(store);
        return Result.ok(ResultCode.SUCCESS,page1.getTotal(),stores);
    }

    /**
     * 添加分店
     * @param user
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "addSubStore", method = RequestMethod.POST)
    @ApiOperation(value = "添加分店", notes = "添加分店", httpMethod = "POST")
    public Result addSubStore(Store store) throws Exception{
        System.out.println(store);
        int i = storeServiceImpl.addSubStore(store);
        if(i>0){
            return Result.ok(ResultCode.SUCCESS, i);
        }else{
            return Result.ok(ResultCode.FAIL, null);
        }
    }

    @ApiOperation(value = "查看分店",httpMethod = "GET")
    @RequestMapping(value = "lookSubStore/{zStoreCode}",method = RequestMethod.GET)
    public Result lookSubStores(Integer page, Integer limit, String searchParams, @PathVariable("zStoreCode") int zStoreCode) throws Exception{

        ObjectMapper objectMapper = new ObjectMapper();
        Store store = null;
        if (searchParams!=null){
            store = objectMapper.readValue(searchParams, Store.class);
        }
        Page page1 = PageHelper.startPage(page, limit);
        List<Store> stores = storeServiceImpl.lookSubStore(zStoreCode,store);
        return Result.ok(ResultCode.SUCCESS,page1.getTotal(),stores);
    }


}
